<?php
/**
 * Created by JetBrains PhpStorm.
 * User: pietrovivoli
 * Date: 15/06/13
 * Time: 19:31
 * To change this template use File | Settings | File Templates.
 */






function store_mail ($mail_address) {

    //$path = ${_SERVER['DOCUMENT_ROOT']};

    include($_SERVER['DOCUMENT_ROOT'] . '/installdb/class.DBmanager.php');

    //echo $_SERVER['DOCUMENT_ROOT'];

    /* MYSQLi */
    $db = DBManager::getInstance();

    $conn = $db->db_connect_mysqli();


    // check if already exists in DB
    $query = "SELECT mail_address
              FROM users_prereg
              WHERE mail_address = '$mail_address' ";

    try {
        $result = $db->db_query_mysqli($query);

        if (mysqli_num_rows($result) === 0) { // new user

            $query = "INSERT INTO users_prereg (mail_address)
                VALUES ('$mail_address')";

            $result = $db->db_query_mysqli($query);
        }

        return true;
    }
    catch (Exception $e) {
        return false;
    }

}


function send_mail_tracking ($mail) {

    $to = 'contact@giftshake.it';
    //$to = 'pietro.vivoli@gmail.com';
    $ccn = '';
    $subject = "Accesso alla demo";
    $bodymail = "Utente con indirizzo mail: " . $mail . " ha eseguito l'accesso alla demo" ;


    // invio la mail
    $result = send_mail($to, $ccn, $subject, $bodymail);

    return $result;

}



function send_mail_tracking_survey ($stringa) {

    $to = 'contact@giftshake.it';
    //$to = 'pietro.vivoli@gmail.com';
    $ccn = '';
    $subject = "Survey completata";
    $bodymail = "Un utente ha concluso la survey. Stringa: " . $stringa ;


    // invio la mail
    $result = send_mail($to, $ccn, $subject, $bodymail);

    return $result;

}




function send_mail ($to, $ccn, $subject, $bodymail) {

    require_once ('./phpmailer/class.phpmailer.php');

    $mail = new PHPMailer();


    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = "smtp.giftshake.it";                    // Specify main and backup server
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'contact@giftshake.it';                            // SMTP username
    $mail->Password = 'pietr0flavi01';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted



    // The from email address should be accepted by the smtp server
    $mail->From = "contact@giftshake.it";
    $mail->FromName = "Giftshake";

    $mail->AddAddress($to);

    if (empty($ccn))
        $mail->AddBCC($ccn);

    //$mail->AddAddress("pietro.vivoli@gmail.com");
    //$mail->AddReplyTo('contact@giftshake.it', 'Contact');

    $mail->Subject = $subject;
    $mail->Body = $bodymail;

    $mail->IsHTML(true);
    //$mail->AddEmbeddedImage("img.gif", "img", "img.gif");
    //$mail->Body .= 'Embedded Image: <img alt="My Logo" src="cid:img"> Here is an image!';
    $mail->WordWrap = 250;

    //$mail->Send();


      if(!$mail->Send())
      {
          return false;
         // echo "Message was not sent";
         // echo "Mailer Error: " . $mail->ErrorInfo;
      }
      /*else
      {
         // echo "Message has been sent";
      }*/

     return true;

}





?>